A friend at work was recently hoping to buy his first house and asked me where to look. “That depends on what you need”, I said, like a typical consultant. But it got me thinking.

Knowing the criteria in question, I had some general views, but they didn’t even convince me. So I turned to data. Fortunately, I could scrape data on the criteria I sought for all English districts:

I then pulled all this together into a weighted desirability score and mapped it. And, thinking optimistically, it must have helped my friend, as he ended-up buying a property in district rated as highly desirable.

Here are the results:



I’ve also built this Shiny app to add interaction to this map, enabling users to alter the weights and select only coastal regions, should they wish.

And here’s the code for the map above:

library(ggplot2)
library(dplyr)
library(ggmap)
library(readr)
library(leaflet)

setwd("~/GitHub/p0bs.github.io/")

ScoredAreas1a <- read_csv("ScoredAreas1a.csv", 
                          col_types = c("iicddldddddidddddd"))
Areas1.df <- read_csv("Areas1df.csv", 
                      col_types = c("iicddilic"))

TopAreas <- ScoredAreas1a %>%
  mutate(score = -1*((15*z.time) + 
                       (1*z.age) + 
                       (6*z.expense) + 
                       (6*z.crime) + 
                       (6*z.flunkers) + 
                       (3*z.deprived))) %>% 
  arrange(desc(score), id) %>% 
  left_join(Areas1.df, by="id") %>%
  rename(lng=long)

colnames(TopAreas)[24] <- "Xorder"

LngLat1 <- TopAreas %>% 
  arrange(desc(score), id, Xorder) %>% 
  select(lng, lat, id, Xorder, score) %>% 
  mutate(Nid0a = substring(id, 1, 1)) %>% 
  mutate(Nid0b = as.numeric(substring(id, 2)))

LngLat1a1 <- substring(LngLat1$id, 1, 1)
LngLat1a2 <- substring(LngLat1$id, 2)
Nid <- as.numeric(LngLat1a2)
Nid1 <- unique(Nid)
Nid1 <- Nid1 + 0.5

LngLat2 <- LngLat1 %>% 
  select(lng, lat, score, Nid0b)

lLngLat3 <- length(Nid1)

LngLat3a <- rep(NA, lLngLat3)
LngLat3b <- rep(NA, lLngLat3)
LngLat3c <- rep(NA, lLngLat3)

LngLat3 <- cbind(LngLat3a, LngLat3b, LngLat3c, Nid1)
colnames(LngLat3) <- c("lng", "lat", "score", "Nid0b")

LngLat4 <- rbind(LngLat2, LngLat3)

LngLat4b <- LngLat4 %>% 
  arrange(Nid0b) 

LngLat5 <- LngLat4b %>% 
  select(lng, lat)

LngLat <- as.matrix(LngLat5, ncol=2)

Score1 <- LngLat4b %>% 
  select(Nid0b, score) %>% 
  distinct()

Score1 <- Score1[complete.cases(Score1),]
Score1 <- as.matrix(Score1, ncol=2)
Score1 <- as.data.frame(Score1)

pal <- colorQuantile("YlOrRd", NULL, n = 16)

leaflet(LngLat) %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  setView(lng = -0.3, lat = 51.3, zoom = 8) %>% 
  addPolygons(fillColor = pal(Score1$score), 
              popup = paste("Overall score: ", 
                            round(Score1$score,2)),
              weight = 1,
              color = "grey",
              opacity = 0.7,
              smoothFactor = 0.5)  %>% 
  addLegend("bottomright", pal = pal, 
            values = Score1$score,
            title = "Desirability",
            labFormat = labelFormat(),
            opacity = 1)

Copyright © 2016 Robin Penfold. Built using Rmarkdown and GitHub pages.